package com.xiaosu.red_travel.controller;


import com.xiaosu.red_travel.bean.ResultInfo;
import com.xiaosu.red_travel.group.Group;
import com.xiaosu.red_travel.mapper.User;
import com.xiaosu.red_travel.service.UserService;
import com.xiaosu.red_travel.util.FileToUtil;
import com.xiaosu.red_travel.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;


@RestController
@CrossOrigin
public class UserController {

    @Autowired
    public UserService userService;

    /**
     * 用户注册
     * @param user 用户信息
     * @return 结果对象
     */
    @RequestMapping("/register")
    public ResultInfo register(@Validated({Group.Register.class}) User user){
        user.setPassword(MD5Util.stringToMD5(user.getPassword()));
        boolean save = userService.save(user);
        if(save){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no();
        }
    }

    /**
     * 用户登录
     * @param user 用户名和密码
     * @param session
     * @return 结果对象
     */
    @RequestMapping("/login")
    public ResultInfo login(@Validated({Group.Login.class}) User user, HttpSession session){
        user.setPassword(MD5Util.stringToMD5(user.getPassword()));
        User loginUser = userService.login(user);
        if(loginUser==null){
            return ResultInfo.no("用户名或者密码错误");
        }else {
            session.setAttribute("login_user"+loginUser.getId(),loginUser);
            loginUser.setPassword("");
            return ResultInfo.ok(session.getId()+"",loginUser);
        }
    }

    /**
     * 用户退出
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/tuichu")
    public ResultInfo returnLogin(Integer id,HttpSession session){
        session.removeAttribute("login_user"+id);
        return ResultInfo.ok("退出成功");
    }

    /**
     * 修改用户
     * @param user 用户对象
     * @return 结果对象
     */
    @RequestMapping("/updateUser")
    public ResultInfo update(@Validated({Group.Update.class}) User user){
        //把用户密码转换为MD5
        user.setPassword(MD5Util.stringToMD5(user.getPassword()));
        boolean b = userService.updateById(user);
        if(b){
            return ResultInfo.ok("修改成功");
        }else {
            return ResultInfo.no("修改失败");
        }
    }

    /**
     * 用户上传图片
     * @param file 图片文件
     * @param session
     * @return 结果对象
     */
    @RequestMapping("/uploadHeadPic")
    public ResultInfo upLoadHeadPic(@RequestParam(value = "filename") MultipartFile file,HttpSession session){
        try {
           // 上传图片到阿里云oss

            String s = FileToUtil.updateImg(file.getOriginalFilename()+new Random().nextInt(10), file.getInputStream());
            return ResultInfo.ok(s);
        } catch (IOException e) {
            return ResultInfo.no("图片上传失败");
        }
    }


    /**
     * 验证用户是否登录
     * @param id 用户id
     * @param session
     * @return 结果对象
     */
    @RequestMapping("/ifLogin")
    public ResultInfo ifLogin(Integer id,HttpSession session){
        User loginUser = (User) session.getAttribute("login_user" + id);
        if(loginUser==null){
            return ResultInfo.no("用户未登录");
        }else {
            return ResultInfo.ok("用户已经登录");
        }
    }

}
